{extend name="public/layout" /}
{block name="main"}
<form class="layui-form" action="{$action}" method="post" enctype="multipart/form-data" style="margin-top: 50px;margin-left: 100px;margin-bottom: 100px">
    {foreach $data as $value}

    {if $value['type']=='input'}
    {include file="form/input" /}
    {/if}

    {if $value['type']=='password'}
    {include file="form/password" /}
    {/if}

    {if $value['type']=='none'}
    {include file="form/none" /}
    {/if}

    {if $value['type']=='select'}
    {include file="form/select" /}
    {/if}

    {if $value['type']=='checkbox'}
    {include file="form/checkbox" /}
    {/if}

    {if $value['type']=='switch'}
    {include file="form/switch" /}
    {/if}

    {if $value['type']=='radio'}
    {include file="form/radio" /}
    {/if}

    {if $value['type']=='textarea'}
    {include file="form/textarea" /}
    {/if}

    {if $value['type']=='img'}
    {include file="form/img" /}
    {/if}

    {if $value['type']=='imgs'}
    {include file="form/imgs" /}
    {/if}

    {if $value['type']=='text'}
    {include file="form/text" /}
    {/if}

    {if $value['type']=='editor'}
    {include file="form/editor" /}
    {/if}

    {if $value['type']=='editors'}
    {include file="form/editors" /}
    {/if}

    {if $value['type']=='file'}
    {include file="form/file" /}
    {/if}

    {if $value['type']=='video'}
    {include file="form/video" /}
    {/if}

    {if $value['type']=='files'}
    {include file="form/files" /}
    {/if}

    {if $value['type']=='tree'}
    {include file="form/tree" /}
    {/if}

    {if $value['type']=='time'}
    {include file="form/time" /}
    {/if}

    {if $value['type']=='optgroup'}
    {include file="form/optgroup" /}
    {/if}

    {if $value['type']=='json'}
    {include file="form/json" /}
    {/if}

    {if $value['type']=='inclass'}
    {include file="form/inclass" /}
    {/if}

    {/foreach}
    {include file="form/button" /}
</form>
{/block}
{block name="script"}
<script src="__STATIC__/admin/fuwenben/ueditor.config.js"></script>
<script src="__STATIC__/admin/fuwenben/ueditor.all.js"></script>
<script src="__STATIC__/admin/fuwenben/lang/zh-cn/zh-cn.js"></script>
<script src="__STATIC__/admin/js/jquery-2.2.3.min.js"></script>
<script src="__STATIC__/admin/js/qiniu.min.js"></script>
<script src="__STATIC__/admin/js/ChaJian.js"></script>
<script src="__STATIC__/admin/js/MaoWenKe.js"></script>
<script>
    var layedit = layui.layedit;
    var editor = $(".editor");
    var layuiedit = $(".editors");
    var maowenke = new MaoWenKe();

function hesd(){
    console.log('haha')
}
    /**
     * 添加编辑框
     * */
    layedit.set({
        uploadImage:{
            url: '{:url("api/editorimgs")}' //接口url
            , type: 'POST' //默认post
        },
        height:500,
    })
    $.each(editor,function (i,v) {
        var editors = $(v).attr('name');
        var ue = UE.getEditor(editors);
        // console.log(editors);
        // layedit.build(editors);
    })
    $.each(layuiedit,function (i,v) {
        var ids = $(v).attr('id');
        layedit.build(ids);
    });
    layui.config({
        base:'__STATIC__/admin/layui/ajaxCascader/'
        ,version: '1.6'
    })
    layui.use(['form','ajaxCascader'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var timesss = $(".timesss");
        var vstr = '';
        var stimeid = '';
        var cascader =layui.ajaxCascader;

        //加载无限联动框
        var inclass = $('.inclass');
        $.each(inclass,function (i,v) {
            var id = $(v).attr('name');
            var  action = $(v).attr('data-action');
            var pidfield = $(v).attr('pid');
            var myvalue = $(v).attr('newvalue');
            if(myvalue){
                myvalue = myvalue.split(',');
            }else{
                myvalue = [];
            }
            console.log(myvalue);
            // console.log(action);
            // Ajax传参模式
            var inclasss = cascader.load({
                elem: '#'+id
                ,search: {
                    show: true,
                    minLabel: 3,
                    placeholder: '请输入搜索词'
                }
                ,showlast:true
                ,clear: true
                ,value: 0
                ,getChildren: function(value,callback){
                    console.log(value);
                    $.post(action+'?id='+value+'&pidfield='+pidfield,function (res) {
                        console.log(res);
                        if(res.code==200){
                            var data = res.data;
                            for(var i in data){
                                data[i].value = data[i].id;
                                data[i].label = data[i].name;
                                delete data[i].id;
                                delete data[i].name;
                                data[i].hasChild = true;
                            }
                            callback(data);
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                },
                chooseData:myvalue
            });
            cascader.on('click',"#"+id,function () {
                var data = cascader.getChooseData("#"+id);
                data = data[data.length-1];
                console.log($("[name="+id+"]").val(data));
            })

        })


        $.each(timesss,function (i,v) {
            stimeid = $(v).attr('id');
            vstr = laydate.render({
                elem: "#"+stimeid,
                type:'datetime'
            })
        })
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
        form.on("select(daochu)",function (data) {
            console.log(data);
        })
    });
    var tree = layui.tree;
//    var util = layui.util;
    var treesubject = $(".tree");
    var treeid = '';
    var treeoption = '';
    var abs = '';
    $.each(treesubject,function (i,v) {
        treeid = $(v).attr('id');
        treeoption = $(v).next('.value').eq(0).children('.option').text();
        treeoption = JSON.parse(treeoption);
        // console.log(treeoption)
        // console.log(treeid)
        abs = tree.render({
            elem:"#"+treeid,
            data:treeoption,
            showCheckbox:true,
            isJump:true,
            onlyIconControl:true,
            id:'id',
            click:function (data) {
                console.log('haha');
            }
        })
        $("[type=submit]").attr('onclick','huoqushuju(this)')
        $("[type=submit]").attr('type','button');
    });
    /**
     * 获取树形组建数据
     * */
    function huoqushuju(obj) {
        var name = $(obj).attr('data-name');
        var data = abs.getChecked();
        var newdata = [];
        $.each(data,function (i,v) {
            if(v.children){
                $.each(v.children, function (is,vs) {
                    newdata.push(vs.id);
                })
            }
        })

        if(newdata.length>0){
            newdata = newdata.join(',');
        }
        console.log(newdata);
        var name = $('.lay_tree').attr('data-name');
        var str = "<input type='text' name='"+name+"' value='"+newdata+"' />";
        $('.lay_tree').append(str);
        $("[type=button]").attr('type','submit');
    }

    function back() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

        parent.layer.close(index); //再执行关闭
    }

    // tree.render({
    //     elem: '#test12'
    //     ,data: data
    //     ,showCheckbox: true  //是否显示复选框
    //     ,id: 'demoId1'
    //     ,isJump: true //是否允许点击节点时弹出新窗口跳转
    //     ,click: function(obj){
    //         var data = obj.data;  //获取当前点击的节点数据
    //         layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
    //     }
    // });


    function mimas(obj) {
        var type = $(obj).parent().prev('input').attr('type');
        if(type=='password'){
            type = 'text';
        }else{
            type = 'password';
        }
        $(obj).parent().prev('input').attr('type',type);
        // console.log(type);
    }
    /**
     * 图片预览和上传
     * @param obj
     */
    function yulanimg(obj) {
        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        ChaJian.upFile($(obj),"{:url('api/upimg')}",function (res) {
            console.log(res);
            layer.close(index);
            if(res.code==200){
                layer.msg(res.msg);
                var str = '';
                $.each(res.data,function (i,v) {
                    str +='<img src="'+v.path+'" style="max-width: 200px;max-height: 100px"/>';
                    $(obj).prev('input').val(v.id);
                })
                $(obj).parents('.layui-input-block').next().empty();
                $(obj).parents('.layui-input-block').next().append(str);
            }else{
                layer.msg(res.msg);
            }
        })
    }

    /**
     * 添加多图片
     * @param obj
     */
    function yulanimgs(obj) {
        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        ChaJian.upFile($(obj),"{:url('api/upimg')}",function (res) {
            // console.log(res);
            layer.close(index);
            if(res.code==200){
                layer.msg(res.msg);
                var str = '';
                var value = [];
                $.each(res.data,function (i,v) {
                    str +='<div style="float: left;margin-right: 5px" class="dels">\n' +
                        '            <img src="'+v.path+'" style="max-width: 200px;max-height: 100px"/>\n' +
                        '            <div class="zhong">\n' +
                        '                <input style="" type="button" onclick="shanchuimg('+v.id+',this)" class="layui-btn layui-btn-sm layui-btn-danger" value="删除">\n' +
                        '            </div>\n' +
                        '        </div>';
                    value.push(v.id);
                })
                var newvalue = $(obj).prev('input').val();
                if(!newvalue){
                    newvalue = [];
                }else{
                    newvalue = newvalue.split(',');
                }
                console.log(value);
                newvalue = ChaJian.addArray(newvalue,value);
                newvalue = newvalue.join(',');
                $(obj).prev('input').val(newvalue);
                $(obj).parents('.layui-input-block').next().append(str);
            }else{
                layer.msg(res.msg);
            }
        })
    }

    /**
     * 删除图片
     * @param id
     * @param obj
     */
    function shanchuimg(id,obj) {
        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        $.post("{:url('api/deleteimg')}",{id,id},function (res) {
            layer.close(index);
            if(res.code==200){
                layer.msg(res.msg);
                var array = $(obj).parents('.shows').prev('.layui-input-block').children('.Mfiles').children('input').eq(0).val();
                // var array = $(obj).parents('.shows').prev('.layui-input-block').children('.Mfile');
                console.log(array);
                array = array.split(',');
                array = ChaJian.delArray(array,[id]);
                array = array.join(',');
                console.log(array);
                // console.log($(obj).parents('.shows').prev('.layui-input-block').children('.Mfile').children('input').eq(0).val())
                $(obj).parents('.shows').prev('.layui-input-block').children('.Mfiles').children('input').eq(0).val(array)
                $(obj).parents('.dels').remove();
                // console.log($(obj).parents('.dels'))
            }else{
                layer.msg(res.msg);
            }
        })
    }

    /**
     * 单文件分片上传
     * @param obj
     */
    function fenpianshangchuan(obj) {
        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        console.log(maowenke);
        maowenke.fenpianFile($(obj),"{:url('api/fenpian')}",function (res) {
            console.log(res);
            layer.msg('上传完成!!')
            layer.close(index);
            var str = '有1个文件';
            $(obj).next().empty();
            $(obj).next().html(str);
            $(obj).prev('input').val(res[0].id);
        },function (res) {
            // console.log(res);
            console.log('上传一个')
        },function (res) {
            console.log('上传一片')
        })
    }

    /**
     * 多文件分片上传
     * @param obj
     */
    function fenpianshangchuans(obj){
        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        maowenke.fenpianFile($(obj),"{:url('api/fenpian')}",function (res) {
            console.log(res);
            layer.msg('上传完成!!')
            layer.close(index);
            var str ='有'+res.length+ '个文件'
            var id = [];
            $.each(res,function (i,v) {
                id.push(v.id);
            })
            $(obj).next().empty();
            $(obj).next().html(str);
            if(id.length>0){
                $(obj).prev('input').val(id.join(','));
            }
        },function (res) {
            // console.log(res);
            console.log('上传一个')
        },function (res) {
            console.log('上传一片')
        })
    }
    //七牛单文件上传文件
    function qiniufenpian(obj) {
        var index = layer.load(0, {shade: false});
        var observer = {
            next(res){
                // ...
                console.log(res);
                console.log('上传一片');
            },
            error(err){
                // ...
            },
            complete(res){
                var str = res.quan_path;
                $(obj).next().empty();
                $(obj).next().append(str);
                $(obj).prev('input').val(res.id);
                layer.close(index);
                layer.msg('上传完成');
            }
        }
        ChaJian.QiNiuUpfile("{:url('api/addqiniufile')}",$(obj),observer);
    }
    //七牛上传多文件
    function qiniufenpians(obj) {
        var index = layer.load(0, {shade: false});
        var observer = {
            next(res){
                // ...
                console.log(res);
                console.log('上传一片');
            },
            error(err){
                // ...
            },
            complete(res){
                console.log(res);
                // var str = res.quan_path;
                // $(obj).next().empty();
                // $(obj).next().append(str);
                // $(obj).prev('input').val(res.id);
            },
            wc(ress){
                layer.close(index);
                layer.msg('上传完成');
            }
        }
        ChaJian.QiNiuUpfile("{:url('api/addqiniufile')}",$(obj),observer,0);
    }

    /**
     * 添加json数组框
     * @param obj
     */
    function tianjiajson(obj) {
        var name = $(obj).attr('data-name');
        var option0 = $(obj).attr('data-option0');
        var option1 = $(obj).attr('data-option1');
        console.log(name);
        console.log(option0);
        console.log(option1);
        var str = '<div class="layui-col-xs12 queding" style="margin-top: 10px">\n' +
            '            <div class="layui-col-xs4"><input type="text" name="'+name+'['+option0+'][]" class="layui-input" ></div>\n' +
            '            <div class="layui-col-xs4 layui-col-xs-offset1"><input type="text" name="'+name+'['+option1+'][]" class="layui-input" ></div>\n' +
            '            <div class="layui-col-xs3" style="text-align: center;line-height: 38px;cursor: pointer;color: red" onclick="jiesaojson(this)">减少</div>\n' +
            '        </div>';
        $(obj).parents('.tianjia').append(str)
        // console.log($(obj).parents('.queding').after(str));
    }

    /**
     * 删除json数组框
     * @param obj
     */
    function jiesaojson(obj) {
        $(obj).parents('.queding').remove();
    }

    /**
     * 添加视频
     * @param obj
     */
    function upvideos(obj) {
        // var id= $(obj).parent().prev().val();
        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        maowenke.arrayFenPianFile($(obj),"{:url('api/upvideo')}",{},function (res) {
            layer.close(index);
            console.log(res);
            var data = res[0];
            console.log(data);
            var str = '<video src="'+data['path']+'" height="100"  controls="controls" style="float: left;margin-left: 5px"></video>';
            $(obj).parent('.Mfiles').next().children('div').eq(0).html(str);
            $(obj).parent().prev().val(data.id);
            maowenke.forEachVideo();
        },function () {

        },function (res) {
            console.log(res);
        })
    }
    function  qiniuupvideos(obj) {
        var index = layer.load(0, {shade: false});
        var observer = {
            next(res){
                // ...
                console.log(res);
                console.log('上传一片');
            },
            error(err){
                // ...
            },
            complete(res){
                var str = res.quan_path;
                var str = '<video src="'+str+'" height="100"  controls="controls" style="float: left;margin-left: 5px"></video>';
                $(obj).parent('.Mfiles').next().children('div').eq(0).html(str);
                $(obj).parent().prev().val(res.id);
                // $(obj).parent().prev().val(data.id);
                // $(obj).next().empty();
                // $(obj).next().append(str);
                // $(obj).prev('input').val(res.id);
                layer.close(index);
                layer.msg('上传完成');
            }
        }
        ChaJian.QiNiuUpfile("{:url('api/addqiniufile')}",$(obj),observer);
    }
    function reload() {
        window.location.reload();
    }


</script>
{/block}